package dev.alm.functioncallchatmodel.config;

import dev.alm.functioncallchatmodel.component.RedisChatMemoryStore;
import dev.alm.functioncallchatmodel.service.ChatService;
import dev.alm.functioncallchatmodel.service.FunctionService;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.request.json.JsonObjectSchema;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolExecutor;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class LLMConfig {

    @Value("${llm.api.key}")
    private String apiKey;

    @Value("${llm.model.name}")
    private String modelName;

    @Value("${llm.base.url}")
    private String baseUrl;

    @Resource
    private RedisChatMemoryStore redisChatMemoryStore;

    @Bean
    public OpenAiChatModel openAiChatModel() {
        return OpenAiChatModel.builder()
                .apiKey(apiKey)
                .modelName(modelName)
                .baseUrl(baseUrl)
                .build();
    }

    @Bean
    public OpenAiStreamingChatModel openAiStreamingChatModel() {
        return OpenAiStreamingChatModel.builder()
                .apiKey(apiKey)
                .modelName(modelName)
                .baseUrl(baseUrl)
                .build();
    }

    @Bean
    public ChatService chatService(OpenAiStreamingChatModel openAiStreamingChatModel) {
        ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(100)
                .chatMemoryStore(redisChatMemoryStore)
                .build();
        return AiServices.builder(ChatService.class)
                .streamingChatModel(openAiStreamingChatModel)
                .chatMemoryProvider(chatMemoryProvider)
                .build();
    }

    @Bean
    public FunctionService functionService(OpenAiChatModel openAiChatModel) {

        // 工具说明：用于描述工具的作用
        ToolSpecification toolSpecification = ToolSpecification.builder()
                .name("create_invoice")
                .description("根据用户提供的开票信息，开具发票")
                .parameters(JsonObjectSchema.builder()
                        .addStringProperty("companyName", "公司名称")
                        .addStringProperty("dutyNumber", "税号序列")
                        .addStringProperty("amount", "开票金额，保留两位数字")
                        .build()
                ).build();

        // 业务逻辑
        ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> {
            System.out.println(toolExecutionRequest.id());
            System.out.println(toolExecutionRequest.name());
            String arguments = toolExecutionRequest.arguments();
            System.out.println("arguments = " + arguments);
            return "开具成功";
        };


        return AiServices.builder(FunctionService.class)
                .chatModel(openAiChatModel)
                .tools(Map.of(toolSpecification, toolExecutor))
                .build();

    }

}

